package com.mrent.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.mrent.entity.Goods;
import com.mrent.util.Constance;

@Component
public class GoodsServiceImpl implements GoodsService {
	
	/**
	 * 保存商品.
	 * @param goods
	 */
	public void saveGoods(Goods goods){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try{
			trans.begin();
			em.persist(goods);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			trans.commit();
		}
	}
	
	/**
	 * 修改商品.
	 * @param goods
	 */
	public void updateGoods(Goods goods){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try{
			trans.begin();
			em.merge(goods);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			trans.commit();
		}
	}
	
	/**
	 * 获取商品.
	 * 
	 * @param goodsId
	 * @return
	 */
	public Goods getGoods(Long goodsId){
		EntityManager em = EMF.getEM();
		Goods goods = null;
		try{
			goods = em.find(Goods.class, goodsId);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return goods;
	}
	
	/**
	 * 通过商品编号获取商品.
	 * 
	 * @param goodsNo
	 * @return
	 */
	public Goods getGoods(String goodsNo){
		EntityManager em = EMF.getEM();
		Query query = em.createQuery("select from Goods g where g.goodsNo = :goodsNo");
		query.setParameter("goodsNo", goodsNo);
		Goods goods = (Goods)query.getSingleResult();
		return goods;
	}
	
	/**
	 * 删除商品，改状态，不物理删除.
	 * 
	 * @param goodsId
	 */
	public void delGoods(Long goodsId){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try{
			trans.begin();
			Goods goods = em.find(Goods.class, goodsId);
			if(goods !=null ){
				goods.setIsDelete(Constance.IS_DEL);
				em.merge(goods);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			trans.commit();
		}
	}
	
	
	/**
	 * 商品列表.
	 * 
	 * @param page
	 * @return
	 */
	public List<Goods> listGoods(Page page,String manager){
		EntityManager em = EMF.getEM();
		Query query = em.createQuery("select from Goods g where g.manager =:manager and g.isDelete =0");
		query.setParameter("manager", manager);
		page.setTotalCount(query.getResultList().size());
		query.setFirstResult(page.getFirst());
		query.setMaxResults(page.getPageSize());
		List<Goods> list = query.getResultList();
		return list;
	}
	
	/**
	 * 拍卖商品列表.
	 * 
	 * @param page
	 * @return
	 */
	public List<Goods> listAuctionGoods(Page page,String manager){
		EntityManager em = EMF.getEM();
		Query query = em.createQuery("select from Goods g where g.manager =:manager and g.isAuction=1 and g.isDelete =0");
		query.setParameter("manager", manager);
		page.setTotalCount(query.getResultList().size());
		query.setFirstResult(page.getFirst());
		query.setMaxResults(page.getPageSize());
		List<Goods> list = query.getResultList();
		return list;
	}
	
	/**
	 * 最新商品列表,不包括拍卖.
	 * 
	 * @param page
	 * @return
	 */
	public List<Goods> listNewGoods(Page page,String manager){
		EntityManager em = EMF.getEM();
		Query query = em.createQuery("select from Goods g where g.manager =:manager and g.isAuction=0 and g.isDelete =0 order by g.addTime desc");
		query.setParameter("manager", manager);
		page.setTotalCount(query.getResultList().size());
		query.setFirstResult(page.getFirst());
		query.setMaxResults(page.getPageSize());
		List<Goods> list = query.getResultList();
		return list;
	}
	
	/**
	 * 取镇店之宝.
	 * @param manager
	 * @return
	 */
	public Goods getShopBaby(String manager){
		EntityManager em = EMF.getEM();
		Query query = em.createQuery("select from Goods g where g.manager =:manager and g.isShopBaby =1");
		query.setParameter("manager", manager);
		Goods goods = null;
		try{
			List list = query.getResultList();
			if(list.size() > 0){
				Object obj = query.getSingleResult();
				if(obj != null){
					goods = (Goods)obj;
				}
			}
			
		}catch(NoResultException e){
			
		}
		return goods;
	}
	
	/**
	 * 判断是否已存在镇店之宝.
	 * 
	 * @param manager
	 * @param goodsId
	 * @return
	 */
	public boolean haveShopBaby(String manager,Long goodsId){
		boolean isHave = false;
		EntityManager em = EMF.getEM();
		Query query = em.createQuery("select from Goods g where g.manager =:manager and g.isShopBaby =1");
		query.setParameter("manager", manager);
		List<Goods> list = query.getResultList();
		if(list.size() > 0){
			for(Goods goods : list){
				if(goods.getGoodsId().intValue() != goodsId.intValue()){
					isHave = true;
					return isHave;
				}else{
					continue;
				}
			}
		}else{
			return isHave;
		}
		return isHave;
	}

}
